#ifndef NATIVE_CAMERA_MANAGER_H
#define NATIVE_CAMERA_MANAGER_H

#include <string>
#include <vector>
#include <camera/NdkCameraManager.h>
#include <camera/NdkCameraDevice.h>
#include <camera/NdkCameraCaptureSession.h>
#include <camera/NdkCameraMetadata.h>
#include <android/native_window.h>
#include <media/NdkImageReader.h>




class NativeCameraManager {
public:
    NativeCameraManager();
    ~NativeCameraManager();

    bool initialize();
    std::vector<std::string> getAvailableCameras();
    bool openCamera(const std::string& cameraId);
    void closeCamera();
    bool startPreview(ANativeWindow* surface);
    void stopPreview();
    bool takePicture(const std::string& filename);

    bool isCameraOpen() const;

private:
    static void onDeviceDisconnected(void* context, ACameraDevice* device);
    static void onDeviceError(void* context, ACameraDevice* device, int error);
    static void onSessionClosed(void* context, ACameraCaptureSession* session);
    static void onSessionReady(void* context, ACameraCaptureSession* session);
    static void onSessionActive(void* context, ACameraCaptureSession* session);
    static void onImageAvailable(void* context, AImageReader* reader);
    
    // 捕获回调函数 - 用于每一帧的处理
    static void onCaptureStarted(void* context, ACameraCaptureSession* session,
                                const ACaptureRequest* request, int64_t timestamp);
    static void onCaptureProgressed(void* context, ACameraCaptureSession* session,
                                   ACaptureRequest* request, const ACameraMetadata* result);
    static void onCaptureCompleted(void* context, ACameraCaptureSession* session,
                                  ACaptureRequest* request, const ACameraMetadata* result);
    static void onCaptureFailed(void* context, ACameraCaptureSession* session,
                               ACaptureRequest* request, ACameraCaptureFailure* failure);

    // 拍照专用回调函数 - 用于区分拍照帧和预览帧
    static void onPhotoCaptureStarted(void* context, ACameraCaptureSession* session,
                                     const ACaptureRequest* request, int64_t timestamp);
    static void onPhotoCaptureProgressed(void* context, ACameraCaptureSession* session,
                                        ACaptureRequest* request, const ACameraMetadata* partialResult);
    static void onPhotoCaptureCompleted(void* context, ACameraCaptureSession* session,
                                       ACaptureRequest* request, const ACameraMetadata* result);
    static void onPhotoCaptureFailed(void* context, ACameraCaptureSession* session,
                                    ACaptureRequest* request, ACameraCaptureFailure* failure);

    bool createCaptureSession(const std::vector<ANativeWindow*>& surfaces);
    void cleanupCaptureSession();


    ACameraManager* cameraManager;
    ACameraDevice* cameraDevice;
    ACameraCaptureSession* captureSession;
    ACaptureRequest* previewRequest;
    ACameraOutputTarget* previewTarget;
    ANativeWindow* previewSurface;
    AImageReader* imageReader;

    std::string currentCameraId;
    std::string currentPhotoFilename;
    bool isPreviewActive;
};

#endif //NATIVE_CAMERA_MANAGER_H